/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package rc;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import tal.drivechain.variable.Variable;
import tal.drivechain.variable.types.ArrayType;
import tal.drivechain.variable.types.DataType;
import tal.drivechain.variable.types.HashmapType;
import tal.remotecanvas.CanvasEventListener;
import tal.remotecanvas.CanvasHeading;
import tal.remotecanvas.CanvasLocation;
import tal.remotecanvas.CanvasPacket.EVENT;
import tal.remotecanvas.CanvasPacket.FACTORS;

/**
 *
 * @author Tal Eisenberg
 */
public class Events extends rcModule {
    public Events() {
        eventDispatcher.addListener(new anEventListener());
    }

    class anEventListener implements CanvasEventListener {
        private List<Double> accelCoords = new ArrayList<Double>();
        private Variable accelVar = new Variable(EVENT.accelerometer.name(), new ArrayType(DataType.FLOAT), accelCoords, true);

        public void receivedAccelerometer(short[] acceleration) {
            accelCoords.clear();
            accelCoords.add(new Double(acceleration[0]/FACTORS.accelerator.factor));
            accelCoords.add(new Double(acceleration[1]/FACTORS.accelerator.factor));
            accelCoords.add(new Double(acceleration[2]/FACTORS.accelerator.factor));
            sendToOutputs(accelVar, -1);
        }

        private Variable launchVar = new Variable(EVENT.launched.name(), DataType.EVENT, null, true);

        public void serverLaunched() {
            sendToOutputs(launchVar,-1);
        }

        private List<Integer> sizePoint = new ArrayList<Integer>(2);
        private Variable sizeVar = new Variable(EVENT.size.name(), new ArrayType(DataType.INT), sizePoint, true);

        public void receivedSize(short[] size) {
            sizePoint.clear();
            sizePoint.add((int)size[0]);
            sizePoint.add((int)size[1]);
            sendToOutputs(sizeVar, -1);
        }

        private List<Integer> penlocPoint = new ArrayList<Integer>(2);
        private Variable penlocVar = new Variable(EVENT.penloc.name(), new ArrayType(DataType.INT), penlocPoint, true);

        public void receivedPenloc(short[] penloc) {
            penlocPoint.clear();
            penlocPoint.add((int)penloc[0]);
            penlocPoint.add((int)penloc[1]);
            sendToOutputs(penlocVar, -1);
        }

        private HashMap<String, Double> location = new HashMap<String,Double>(6);
        private Variable locationVar = new Variable(EVENT.location.name(), new HashmapType(DataType.FLOAT), location, true);

        public void receivedLocation(CanvasLocation l) {
            location.clear();
            location.put("latitude", l.latitude);
            location.put("longitude", l.longitude);
            location.put("altitude", l.altitude);
            location.put("speed", l.speed);
            location.put("horAccuracy", l.horizontalAccuracy);
            location.put("vertAccuracy", l.verticalAccuracy);
            sendToOutputs(locationVar, -1);
        }

        private HashMap<String, Double> compass = new HashMap<String,Double>(5);
        private Variable compassVar = new Variable(EVENT.compass.name(), new HashmapType(DataType.FLOAT), compass, true);

        public void receivedCompass(CanvasHeading h) {
            compass.clear();
            compass.put("heading", h.heading);
            compass.put("x", h.x);
            compass.put("y", h.y);
            compass.put("z", h.z);
            compass.put("accuracy", h.accuracy);
            sendToOutputs(compassVar, -1);
        }

        Variable createdSpriteVar = new Variable(EVENT.createdsprite.name(), DataType.STRING, null, true);

        public void receivedCreatedSprite(String spriteName) {
            createdSpriteVar.setValue(spriteName, true);
            sendToOutputs(createdSpriteVar, -1);
        }

        private Variable coordVar = new Variable("coord", new ArrayType(DataType.INT), null, true);

        public void receivedTouches(short[][] coords, EVENT type) {
            List<Integer> touches = new ArrayList<Integer>();
            for (short[] c : coords) {
                touches.add((int)c[0]);
                touches.add((int)c[1]);
                coordVar.setName(type.name());
                coordVar.setValue(touches, true);
                sendToOutputs(coordVar, -1);
                touches.clear();
            }
        }

        public void errorEncountered(String s) {
            log("remote-canvas: " + s, MSG.ERROR);
        }
    }
}
